package com.mirror.llk.logic;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.mirror.llk.bean.domain.TTheme;
import com.mirror.llk.bean.request.ThemePublishRequest;
import com.mirror.llk.bean.response.ThemePublishResponse;
import com.mirror.llk.dao.ThemeDao;
import com.mirror.llk.utils.Constant;
import com.mirror.llk.utils.Interface;
import com.mirror.llk.view.ThemeChooseView;
import com.mirror.llk.view.ThemeDownloadView;
import com.mirror.utils.base.Page;
import com.mirror.utils.http.HttpUtils;
import com.mirror.utils.io.FileUtils;
import com.mirror.utils.lang.StringUtils;
import com.mirror.utils.ui.UiUtils;
import com.mirror.utils.xml.XStreamUtils;

public class ThemeLogic {

	private static final String TAG = "ThemeLogic";

	private ThemeDao dao = new ThemeDao();

	public synchronized void saveLocaTheme(TTheme theme) {
		// 存储主题
		Long localId = dao.saveLocal(theme);
		// 存储图片
		String themePath = createLocalThemeDir(localId);
		String tempPath = FileUtils.getFilePath() + Constant.LOCATION_HIGH_PHOTO + Constant.DIR_TEMP_FILE;
		File dir = new File(tempPath);
		List<Bitmap> bitmapList = new ArrayList<Bitmap>();
		File[] files = dir.listFiles();
		for (int i = 0; i < files.length; i++) {
			try {
				FileUtils.writeFile(themePath, StringUtils.substringAfter(files[i].getName(), Constant.IMAGE.PREFIX), new FileInputStream(files[i]));
				if (bitmapList.size() < 4) {
					bitmapList.add(BitmapFactory.decodeStream(new FileInputStream(files[i])));
				}
			}
			catch (Exception e) {
				// 回滚
				FileUtils.deleteDir(themePath);
				dao.deleteLocal(localId);
				return;
			}
		}
		// 生成LOG图
		Bitmap log = UiUtils.mosaicImage(bitmapList, 2);
		FileUtils.saveFile(log, themePath + "0.png");
		// 清空临时目录
		FileUtils.clearDir(tempPath);
	}

	public TTheme queryLocaTheme(Long localId) {
		return dao.queryLocal(localId);
	}

	public ThemePublishResponse publishTheme(ThemePublishRequest request) {
		String backupXml = HttpUtils.post(Interface.THEME_PUBLISH, XStreamUtils.parseObjToXml(request));
		return (ThemePublishResponse) XStreamUtils.parseXmlToObj(backupXml, ThemePublishResponse.class);
	}

	public void queryThemeChoosePage(ThemeChooseView chooseView, Page page) {
		List<TTheme> themeList = dao.queryList(page);
		page.result(themeList);
		chooseView.listView.onStart(page);

		List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
		for (TTheme theme : themeList) {
			HashMap<String, Object> propertyMap = new HashMap<String, Object>();
			propertyMap.put("userNick", theme.getThemeName());
			propertyMap.put("themeName", theme.getThemeName());
			try {
				propertyMap.put("themeImage",
				                BitmapFactory.decodeStream(new FileInputStream(new File(getLocalThemeDir(theme.getLocalId()) + "0.png"))));
			}
			catch (FileNotFoundException e) {
			}
			data.add(propertyMap);
		}
		chooseView.adapter.getData().addAll(data);

		chooseView.adapter.notifyDataSetChanged();
		chooseView.listView.onComplete(page);
	}

	public void queryThemeOfficialPage(ThemeDownloadView downloadView, Page page) {
		List<TTheme> themeList = dao.queryList(page);
		page.result(themeList);
		downloadView.lvOfficial.onStart(page);

		List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
		for (TTheme theme : themeList) {
			HashMap<String, Object> propertyMap = new HashMap<String, Object>();
			propertyMap.put("themeName", theme.getThemeName());
			data.add(propertyMap);
		}
		downloadView.officalAdapter.getData().addAll(data);

		downloadView.officalAdapter.notifyDataSetChanged();
		downloadView.lvOfficial.onComplete(page);
	}

	public void queryThemePrivatePage(ThemeDownloadView downloadView, Page page) {
		List<TTheme> themeList = dao.queryList(page);
		page.result(themeList);
		downloadView.lvPrivate.onStart(page);

		List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
		for (TTheme theme : themeList) {
			HashMap<String, Object> propertyMap = new HashMap<String, Object>();
			propertyMap.put("themeName", theme.getThemeName());
			data.add(propertyMap);
		}
		downloadView.privateAdapter.getData().addAll(data);

		downloadView.privateAdapter.notifyDataSetChanged();
		downloadView.lvPrivate.onComplete(page);
	}

	public String createLocalThemeDir(Long localId) {
		String path = FileUtils.getFilePath() + Constant.LOCATION_HIGH_PHOTO + Constant.DIR_THEME_PHOTO
		        + (String.valueOf(TTheme.THEME_TYPE_PRIVATE) + localId + File.separator);
		FileUtils.createDir(path);
		return path;
	}

	public String getLocalThemeDir(Long localId) {
		return FileUtils.getFilePath() + Constant.LOCATION_HIGH_PHOTO + Constant.DIR_THEME_PHOTO
		        + (String.valueOf(TTheme.THEME_TYPE_PRIVATE) + localId) + File.separator;

	}
}
